package com.xmd.red.utils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/*import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;*/

public class HttpClientUtil {
	
	/**
	 * 发送delete请求
	 *//*
	public static String delete(String url,List<Header> headers){
		CloseableHttpClient client = HttpClients.createDefault();
		HttpDelete delete = new HttpDelete(url);
		HttpEntity entity = null;
		
		if(headers!=null && headers.size()>0){
			for(Header header : headers){
				delete.addHeader(header);
			}
		}
		String result = null;
		try {
			CloseableHttpResponse response = client.execute(delete);
			int i = response.getStatusLine().getStatusCode();
			System.out.println(i);

			result = EntityUtils.toString(response.getEntity(),"UTF-8");
			System.out.println(result);
			// 关闭连接
			EntityUtils.consume(entity);
			response.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	*//**
	 * 使用get请求,参数拼写在url中
	 *//*
	public static String get(String url,List<Header> headers){
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(url);
		HttpEntity entity = null;
		if(headers!=null && headers.size()>0){
			for(Header header : headers){
				get.addHeader(header);
			}
		}
		String result = null;
		try {
			CloseableHttpResponse response = client.execute(get);
			int i = response.getStatusLine().getStatusCode();
			System.out.println(i);

			result = EntityUtils.toString(response.getEntity(),"UTF-8");
			System.out.println(result);
			// 关闭连接
			EntityUtils.consume(entity);
			response.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	*//**
	 * 使用put请求提交json数据
	 *//*
	public static String put(String url,String json,List<Header> headers){
		
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPut put = new HttpPut(url);
		ContentType contentType = ContentType.create("application/json","UTF-8");
		HttpEntity entity = null;
		try {
			if(StringUtils.isNotEmpty(json)){
				entity = new InputStreamEntity(IOUtils.toInputStream(json,"UTF-8"), contentType);
				put.setEntity(entity);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if(headers!=null && headers.size()>0){
			for(Header header : headers){
				put.addHeader(header);
			}
		}
		String result = null;
		try {
			CloseableHttpResponse response = client.execute(put);
			int i = response.getStatusLine().getStatusCode();
			System.out.println(i);

			result = EntityUtils.toString(response.getEntity(),"UTF-8");
			System.out.println(result);
			// 关闭连接
			EntityUtils.consume(entity);
			response.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	*//**
	 * 以json的格式提交post请求
	 *//*
	public static String post(String url,String json,List<Header> headers){
		CloseableHttpClient client = HttpClients.createDefault();

		HttpPost post = new HttpPost(url);
		ContentType contentType = ContentType.create("application/json", "UTF-8");
		HttpEntity entity = null;
		try {
			if(StringUtils.isNotEmpty(json)){
				entity = new InputStreamEntity(IOUtils.toInputStream(json,"UTF-8"), contentType);
				post.setEntity(entity);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if(headers!=null && headers.size()>0){
			for(Header header : headers){
				post.addHeader(header);
			}
		}
		String result = null;
		try {
			CloseableHttpResponse response = client.execute(post);
			int i = response.getStatusLine().getStatusCode();
			System.out.println(i);

			result = EntityUtils.toString(response.getEntity(),"UTF-8");
			System.out.println(result);
			// 关闭连接
			EntityUtils.consume(entity);
			response.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	*//**
	 * 带请求头的表单提交
	 *//*
	public static String post(String url, List<FormBean> params, List<Header> headers) {

		CloseableHttpClient client = HttpClients.createDefault();

		HttpPost post = new HttpPost(url);
		//输入entity;
		HttpEntity entity = null;
		//先判断是否有文件
		boolean flag = false;
		for(FormBean bean : params){
			if(bean.isFile()){
				flag = true;
				break;
			}
		}
		//包含文件,使用多媒体MultipartEntityBuilder
		ContentType contentType = ContentType.create("text/plain", "UTF-8");
		if(flag){
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.setCharset(Charset.forName("UTF-8"));
			if (params != null && params.size() > 0) {
				for (FormBean bean : params) {
					// 添加文件参数
					if (bean.isFile()) {
						builder.addPart(bean.getKey(), new FileBody(bean.getFile()));
					}
					// 添加文本参数
					else {
						builder.addPart(bean.getKey(), new StringBody(bean.getValue(), contentType));
					}
				}
			}
			entity = builder.build();
		}
		//不包含文件,使用普通的EntityBuilder
		else{
			if(params!=null && params.size()>0){
				
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				for(FormBean bean : params){
					nameValuePairs.add(new BasicNameValuePair(bean.getKey(),bean.getValue()));
				}
				try {
					entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		post.setEntity(entity);
		if(headers!=null && headers.size()>0){
			for(Header header : headers){
				post.addHeader(header);
			}
		}
		String result = null;
		try {
			CloseableHttpResponse response = client.execute(post);
			int i = response.getStatusLine().getStatusCode();
			System.out.println(i);
			
			if(i==302){
				String location = response.getHeaders("Location")[0].getValue();
				client = HttpClients.createDefault();
				HttpPost post2 = new HttpPost(location);
				response = client.execute(post2);
			}

			result = EntityUtils.toString(response.getEntity(),"UTF-8");
			System.out.println(result);
			// 关闭连接
			EntityUtils.consume(entity);
			response.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	*//**
	 * 普通的表单提交
	 *//*
	public static String post(String url, List<FormBean> params) {
		
		return post(url,params,null);
	}
	

	public static class FormBean {
		private String key;
		private String value;
		private boolean isFile = false;
		private File file;

		// 文本参数的构造方法
		public FormBean(String key, String value) {
			this.key = key;
			this.value = value;
			this.isFile = false;
			this.file = null;
		}

		// 文件参数的构造方法
		public FormBean(String key, File file) {
			this.key = key;
			this.isFile = true;
			this.file = file;
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public boolean isFile() {
			return isFile;
		}

		public void setFile(boolean isFile) {
			this.isFile = isFile;
		}

		public File getFile() {
			return file;
		}

		public void setFile(File file) {
			this.file = file;
		}
	}*/
}
